/*
 * @Descripttion:
 * @version:
 * @Author: liwanpeng
 * @Date: 2020-12-14 14:46:15
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-03-31 17:48:08
 */
// import "babel-polyfill" // 兼容ie8以上浏览器，zhanglognfei 2020.1.9
import '@tapui/views/microApp/slave/support'
import Vue from 'vue'
import 'normalize.css/normalize.css' // A modern alternative to CSS resets
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import i18n from '@tapui/lang' // internationalization
import '@tapui/styles/common.scss'
import 'animate.css'
import App from './App'
import router from '@tapui/router'
import VueRouter from 'vue-router'
import './router'
import TicUI from 'tic-ui'
import store from '@tapui/store'
import './store'
import TapUI from 'tap-ui'
import misui from 'mis-ui'
import '@tapui/router/merge'
import '@tapui/icons' // icon
import '@/icons' // icon
// import "@tapui/permission" // permission control
import { doFilter } from '@tapui/permission' // permission control

import '@tapui/filter'
import '@tapui/directive'
import '@tapui/utils/topscomm'
import global from './utils/global'
import '@tapui/fonts/iconfont.css'
import './styles/main.scss'
import './styles/mpmProjector.scss'

// store.dispatch('ChangeLoginType', 2)
if (process.env.NODE_ENV === 'preprod') {
  store.dispatch('ChangeLoginType', 1)
} else {
  store.dispatch('ChangeLoginType', 2)
}
store.dispatch('setDisplayMode', 'projector')
store.dispatch('ChangeWebSite', {
  indexTitle: '通用项目管理系统',
  indexSvg: 'logo',
  breadcrumb: '通用项目管理系统',
  firstPage: {
    // 默认系统首页
    title: '首页',
    value: '/dashboard'
  }
})
store.dispatch('setDefaultTheme', '#014c7e')
store.dispatch('setDefaultSize', 'mini')
Vue.use(ElementUI, {
  size: store.getters.size, // set element-ui default size
  i18n: (key, value) => i18n.t(key, value)
})
Vue.config.productionTip = false
store.commit('SET_RECOMMANDCOLOR', ['#409EFF', '#FF5151', '#FE8B3A', '#E626FF', '#0F9A9F', '#CCBC45', '#55CC45', '#ccc'])

TapUI.install(Vue)
TicUI.install(Vue)
misui.install(Vue)
global.install(Vue)
import TlListLeftSearch from '@/components/common/ListLeftSearch'
Vue.component(TlListLeftSearch.name, TlListLeftSearch)
store.dispatch('setDefaultSize', 'small')
store.dispatch('closeSideBar', false)

store.dispatch('AddWhiteList', ['/progressGanttFullScreen', '/progressEchartsFullScreen'])

// 为了做微前端集成，做的变更，如果不需要，则直接注释下列代码，打开最后的new Vue即可
let appId = store.getters.elementIds.app

let instance = null
let defaultRouterBase = ''
let defaultTheme = '#0A966E'
store.dispatch('setDefaultTheme', defaultTheme)
// 独立运行时
if (!store.getters.inMicroApp) {
  new Vue({
    el: '#' + appId,
    router,
    store,
    i18n,
    render: (h) => h(App)
  })
}

// 用于主题切换
let oldTheme
store.dispatch('setMicroAppName', 'gpm')
import { initInfo, microRouter, bindRouter } from '@tapui/views/microApp/slave/utils'
function render(props = {}) {
  const { container, theme, routerBase, token, routers } = props

  defaultRouterBase = routerBase
  if (theme) {
    defaultTheme = theme
  }
  initInfo(props)
  store.dispatch('Token', token)
  store.dispatch('setCloseTheme', true)
  store.dispatch('setTheme', [defaultTheme, oldTheme])
  oldTheme = defaultTheme
  if (instance !== null) {
    return
  }
  let appRoutes = [...routers, ...store.state.permission.externalRouters]
  bindRouter(appRoutes)
  let routerTemp = new VueRouter({
    mode: 'history',
    base: routerBase || `/${store.getters.microAppName}`,
    scrollBehavior: () => ({ y: 0 }),
    routes: [...appRoutes, ...microRouter]
  })
  routerTemp.beforeEach((to, from, next) => {
    doFilter(to, from, next)
  })
  Vue.use(VueRouter)
  instance = new Vue({
    router: routerTemp,
    store,
    render: (h) => h(App)
  }).$mount(container ? container.querySelector('#' + appId) : '#' + appId)
}

export async function bootstrap() {
  // 主应用第一次打开子应用时调用，此时把默认旧样式写为element的默认颜色
  oldTheme = '#409EFF'
}
export async function mount(props) {
  // 每次打开应用时调用
  render(props)
}
export async function unmount() {
  console.log('unamount')
  // 切换子应用时，清空当前应用的信息
  instance.$destroy()
  instance.$el.innerHTML = ''
  instance = null
}
export async function update(props) {
  // 每次更新应用参数时调用
}
